package com.mnkr.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.PreDestroy;
import javax.faces.application.FacesMessage;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.NoneScoped;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;

import com.google.gson.annotations.Expose;


@ManagedBean
@ViewScoped
public class TableBean implements Serializable {  
	  
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final static String[] colors;  
  
    private final static String[] manufacturers;  
  
    static {  
        colors = new String[10];  
        colors[0] = "Black";  
        colors[1] = "White";  
        colors[2] = "Green";  
        colors[3] = "Red";  
        colors[4] = "Blue";  
        colors[5] = "Orange";  
        colors[6] = "Silver";  
        colors[7] = "Yellow";  
        colors[8] = "Brown";  
        colors[9] = "Maroon";  
  
        manufacturers = new String[10];  
        manufacturers[0] = "Mercedes";  
        manufacturers[1] = "BMW";  
        manufacturers[2] = "Volvo";  
        manufacturers[3] = "Audi";  
        manufacturers[4] = "Renault";  
        manufacturers[5] = "Opel";  
        manufacturers[6] = "Volkswagen";  
        manufacturers[7] = "Chrysler";  
        manufacturers[8] = "Ferrari";  
        manufacturers[9] = "Ford";  
    }  
  
    private List<Car> filteredCars;  
  
    private List<Car> cars;  
    
    private Car selectedCar = new Car(); 
    
    private String carModel;
    private boolean update=false;
  
    private Car[] selectedCars;  
    
  
    public TableBean() {  
        cars = new ArrayList<Car>();  
          
        populateRandomCars(cars, 50);  
    }  
    
    
    @PreDestroy
    public void preDestroy(){
    	System.out.println("ACABA DE DESTRUIRSE !!!");
    }
    
    
    public void selectCar(ActionEvent e){
    	System.out.println("seleccionando ActionEvent "+selectedCar.getModel()+ " "+selectedCar.getYear());
    }
    public void selectCar(){
    	System.out.println("seleccionando "+selectedCar.getModel()+ " "+selectedCar.getYear());
    }
  
    public Car getSelectedCar() {  
        return selectedCar;  
    }  
  
    public void setSelectedCar(Car selectedCar) {  
    	try{
    	this.selectedCar = selectedCar.clone();
    	}catch(Exception e){
    		
    	}
    }  
    	
    private void populateRandomCars(List<Car> list, int size) {  
    	list.add(new Car("model1",1991,"fact1","red"));
    	list.add(new Car("model2",1992,"fact2","blue"));
    	list.add(new Car("model3",1993,"fact3","green"));
    	list.add(new Car("model4",1994,"fact4","white"));
    	list.add(new Car("model5",1995,"fact5","black"));
    	list.add(new Car("model1a",1991,"fact1","red"));
    	list.add(new Car("model2a",1992,"fact2","blue"));
    	list.add(new Car("model3a",1993,"fact3","green"));
    	list.add(new Car("model4a",1994,"fact4","white"));
    	list.add(new Car("model5a",1995,"fact5","black"));
    	list.add(new Car("model1b",1991,"fact1","red"));
    	list.add(new Car("model2b",1992,"fact2","blue"));
    	list.add(new Car("model3b",1993,"fact3","green"));
//    	list.add(new Car("model4b",1994,"fact4","white"));
//    	list.add(new Car("model5b",1995,"fact5","black"));
//    	list.add(new Car("model1c",1991,"fact1","red"));
//    	list.add(new Car("model2c",1992,"fact2","blue"));
//    	list.add(new Car("model3c",1993,"fact3","green"));
//    	list.add(new Car("model4c",1994,"fact4","white"));
//    	list.add(new Car("model5c",1995,"fact5","black"));
//    	list.add(new Car("model1d",1991,"fact1","red"));
//    	list.add(new Car("model2d",1992,"fact2","blue"));
//    	list.add(new Car("model3d",1993,"fact3","green"));
//    	list.add(new Car("model4d",1994,"fact4","white"));
//    	list.add(new Car("model5d",1995,"fact5","black"));
//    	list.add(new Car("model1e",1991,"fact1","red"));
//    	list.add(new Car("model2e",1992,"fact2","blue"));
//    	list.add(new Car("model3e",1993,"fact3","green"));
//    	list.add(new Car("model4e",1994,"fact4","white"));
//    	list.add(new Car("model5e",1995,"fact5","black"));
//    	list.add(new Car("model1f",1991,"fact1","red"));
//    	list.add(new Car("model2f",1992,"fact2","blue"));
//    	list.add(new Car("model3f",1993,"fact3","green"));
//    	list.add(new Car("model4f",1994,"fact4","white"));
//    	list.add(new Car("model5f",1995,"fact5","black"));
//        for(int i = 0 ; i < size ; i++)  
//            list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));  
    }  
  
    private int getRandomYear() {  
        return (int) (Math.random() * 50 + 1960);  
    }  
  
    private String getRandomColor() {  
        return colors[(int) (Math.random() * 10)];  
    }  
  
    private String getRandomManufacturer() {  
        return manufacturers[(int) (Math.random() * 10)];  
    }  
  
    private String getRandomModel() {  
        return UUID.randomUUID().toString().substring(0, 8);  
    }  
  
    public List<Car> getFilteredCars() {  
        return filteredCars;  
    }  
  
    public void setFilteredCars(List<Car> filteredCars) {  
        this.filteredCars = filteredCars;  
    }  
  
    public List<Car> getCars() {  
        return cars;  
    }
    
    public void execJSMethod(String method){
    	RequestContext.getCurrentInstance().execute(method);
    }
    
    public void execUpdate(String update){
    	RequestContext.getCurrentInstance().update(update);
    }
    
    public void openNew(){
    	System.out.println("openNew");
    	update=false;
    	selectedCar = new Car();
    	System.out.println(update);
    }
    public void openUpdate(){
    	System.out.println("openUpdate");
    	update=true;
    	System.out.println(update);
    }
    
    public void save(){
    	System.out.println("procesando update: "+update);
    	FacesContext fc = FacesContext.getCurrentInstance();
    	if(update){
    		if(selectedCar.getColor().equals("monkar")){
    			for(int i = 0; i<cars.size();i++){
    				Car car = cars.get(i);
    				if(car.getModel().equals(selectedCar.getModel())){
    					cars.set(i, selectedCar);
    					FacesMessage msg3 = new FacesMessage(FacesMessage.SEVERITY_INFO, "Good, bitch !", null);
    					fc.addMessage(null, msg3);
    					execJSMethod("carDialog.hide();");
    					execUpdate("formMain");
    				}
    			}
    		}else{
    			FacesMessage msg1 = new FacesMessage(FacesMessage.SEVERITY_ERROR, "El color debe ser 'monkar' :D", null);
    			fc.addMessage(null, msg1);	
    		}
    	}else{
    		Car newCar = selectedCar.clone();
    		newCar.setModel(getRandomModel());
    		cars.add(newCar);
    		FacesMessage msg3 = new FacesMessage(FacesMessage.SEVERITY_INFO, "Se agrego correctamente", null);
			fc.addMessage(null, msg3);
			execJSMethod("carDialog.hide();");
			execUpdate("formMain");
    	}
    	
			
    }
    
    public void delete(){
    	System.out.println("entro");
    	FacesContext fc = FacesContext.getCurrentInstance();
		FacesMessage msg3 = new FacesMessage(FacesMessage.SEVERITY_INFO, "Good, bitch !", null);
			for(int i = 0; i<cars.size();i++){
				Car car = cars.get(i);
				if(car.getModel().equals(selectedCar.getModel())){
					cars.remove(i);
					fc.addMessage(null, msg3);
					execJSMethod("carDialog.hide();");
					execUpdate("formMain");
				}
			}
			
    }

	public String getCarModel() {
		return carModel;
	}

	public void setCarModel(String carModel) {
		this.carModel = carModel;
	}


	public boolean isUpdate() {
		return update;
	}


	public void setUpdate(boolean update) {
		this.update = update;
	}
    
    
    
}